package com.cheer.ba.view.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.model.BreakRulesModel;
import com.cheer.ba.model.ResultBreak;
import com.cheer.ba.presenter.impl.CarPresenter;
import com.cheer.ba.utils.A2bigA;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.pickerview.Util;

import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 添加违章查询
 *
 * @author HQ
 *         created at 2017/3/31 0031 16:55
 */
public class BreakRulesCarAty extends BaseCommAty {

    //小型车
    @BindView(R.id.id_small_card_check)
    CheckBox mSmallCar;
    //大型车
    @BindView(R.id.id_large_car_check)
    CheckBox mLargeCar;

    //车牌前缀
    @BindView(R.id.id_car_first_txt)
    TextView mTxtCarLsprefix;
    private String mCarLsprefix;

    //车牌剩余部分
    @BindView(R.id.id_car_card_num_txt)
    EditText mEditCarLsnum;
    private String mCarLsnum;

    //车架号
    @BindView(R.id.id_car_num_txt)
    EditText mEditCarFrameno;
    private String mCarFrameno;
    //车辆品牌
    @BindView(R.id.id_car_type_txt)
    EditText mEditCarType;
    private String mCarType;
    //车辆型号
    @BindView(R.id.id_car_service_txt)
    EditText mEditCarService;
    private String mCarService;

    //存储车辆前缀
    private ArrayList<String> mSaveCarLs = new ArrayList<String>();

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_breakrules, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new CarPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initView() {
        setTitleText(getString(R.string.add_car_info));
        mEditCarLsnum.setTransformationMethod(new A2bigA());
        mEditCarLsnum.setText("A");
        mEditCarLsnum.setSelection(mEditCarLsnum.length());
        mSmallCar.setChecked(false);
        mLargeCar.setChecked(true);
    }

    @Override
    public void initData() {
        String[] s = getResources().getStringArray(R.array.spingarr);
        mSaveCarLs.clear();
        mSaveCarLs.addAll(Arrays.asList(s));
    }

    /**
     * 检查信息是否正确
     *
     * @return
     */
    public boolean checkData() {
        mCarLsprefix = mTxtCarLsprefix.getText().toString().trim();
        mCarLsnum = mEditCarLsnum.getText().toString().trim().toUpperCase();
        mCarFrameno = mEditCarFrameno.getText().toString().trim();
        mCarType = mEditCarType.getText().toString().trim();
        mCarService = mEditCarService.getText().toString().trim();

        if (!mSmallCar.isChecked() && !mLargeCar.isChecked()) {
            SVProgressHUD.showInfoWithStatus(aty, "请选择车型");
        } else {
            if (StringUtils.isEmpty(mCarLsprefix)) {
                SVProgressHUD.showInfoWithStatus(aty, getString(R.string.lsprefix_is_null));
            } else {
                if (StringUtils.isEmpty(mCarLsnum) || mCarLsnum.length() < 6) {
                    SVProgressHUD.showInfoWithStatus(aty, getString(R.string.lsnum_is_null));
                } else {
                    if (StringUtils.isEmpty(mCarFrameno)) {
                        SVProgressHUD.showInfoWithStatus(aty, getString(R.string.frameno_is_null));
                    } else {
                        if (StringUtils.isEmpty(mCarType)) {
                            SVProgressHUD.showInfoWithStatus(aty, getString(R.string.car_type_is_null));
                        } else {
                            if (StringUtils.isEmpty(mCarService)) {
                                SVProgressHUD.showInfoWithStatus(aty, getString(R.string.car_service_is_null));
                            } else {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }

    @OnClick({R.id.id_small_card_check, R.id.id_large_car_check, R.id.id_car_first_txt, R.id.id_bread_rules_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            //小型车
            case R.id.id_small_card_check:
                mSmallCar.setChecked(true);
                mLargeCar.setChecked(false);
                break;
            //大型车
            case R.id.id_large_car_check:
                mSmallCar.setChecked(false);
                mLargeCar.setChecked(true);
                break;
            //车牌号前缀
            case R.id.id_car_first_txt:
                chooseCarNum();
                break;
            //违章查询
            case R.id.id_bread_rules_btn:
                if (checkData()) {
                    ((CarPresenter) presenter).searchCarInfo(mCarLsprefix, mCarLsnum, mCarFrameno);
                }
                break;
        }
    }

    /**
     * 选择车牌号前缀
     */
    private void chooseCarNum() {
        Util.alertBottomWheelOption(aty, mSaveCarLs, new Util.OnWheelViewClick() {
            @Override
            public void onClick(View view, int postion) {
                mCarLsprefix = mSaveCarLs.get(postion);
                mTxtCarLsprefix.setText(mCarLsprefix);
            }
        }, 16);
    }

    @Override
    public <T> void updateView(T t) {
        if (null != t) {
            final ResultBreak rules = (ResultBreak) t;
//            SVProgressHUD.showInfoWithStatus(aty, rules.msg);
            if (null != rules.getRecords()) {
                BreakRulesModel mBreak = new BreakRulesModel();
                if (mSmallCar.isChecked()) {
                    mBreak.mCarType = 1;
                } else {
                    mBreak.mCarType = 2;
                }
                mBreak.mCarLsprefix = mCarLsprefix;
                mBreak.mCarLsnum = mCarLsnum;
                mBreak.mCarFrameno = mCarFrameno;
                mBreak.mCarName = mCarType;
                mBreak.mCarVers = mCarService;
                ((CarPresenter) presenter).saveCarCache(mBreak);

                WatchCarInfoAty.startWatchCarInfoAty(aty, rules);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IntentCode.Break.BREAK_REQUEST_CODE) {
            if (resultCode == IntentCode.General.ATY_FINISH) {
                showActivity(aty, BreakRuleCarListAty.class);
                finish();
            }
        }
    }
}
